#include<iostream>
#include<cmath>
using namespace std;
int fun(int x)
{
	int ma = 0;
	int mi = 20;
	while (x) {
		int k = x % 10;
		ma = max(ma, k);
		mi = min(mi, k);
		x /= 10;
	}
	return ma - mi;
}
int main()
{
	int t;
	cin >> t;
	while (t--) {
		int l, r;
		cin >> l >> r;
		int ma = 0;
		int idx = 0;
		for (int i = l; i <= r; i++) {
			int k = fun(i);
			if (k >= ma) {
				ma = k;
				idx = i;
			}
			if (ma == 9)break;
		}
		cout << idx << endl;
		/*if (r - l >= 100) {
			int ans = r / pow(10, 2);
			if (ans != 1) {
				cout << ans - 1;
			}
			cout << "90" << endl;
		}
		else {
			int ma = 0;
			int idx = 0;
			for (int i = l; i <= r; i++) {
				int x = fun(i);
				if (x > ma) {
					ma = x;
					idx = i;
				}
			}
			cout << idx << endl;
		}*/
	}
	return 0;
}